package pl.put.augrelshooter.activities;

import pl.put.augrelshooter.R;
import pl.put.augrelshooter.adapters.ColorsListAdapter;
import pl.put.augrelshooter.adapters.items.ColorItem;
import pl.put.augrelshooter.dataproviders.NewGameDataProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NewGameActivity extends Activity {
	NewGameDataProvider dataProvider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_game);

		dataProvider = new NewGameDataProvider(this);

		ColorItem[] colors = dataProvider.getAllColorItems();

		ColorsListAdapter colorsAdapter = new ColorsListAdapter(getBaseContext(), colors);

		ListView colorsListView = (ListView) findViewById(R.id.colorsListView);
		colorsListView.setAdapter(colorsAdapter);

		colorsListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
				ColorItem selectedColorItem = (ColorItem) adapter.getItemAtPosition(position);
				dataProvider.setSelectedColor(selectedColorItem.getName());
			}
		});
	}
	
	@Override
    protected void onStop() {
        super.onDestroy();
    }

	public void createGameButtonClicked(View v) {
		Intent gameIntent = new Intent(NewGameActivity.this, GameActivity.class);

		String gameName = ((TextView) findViewById(R.id.newGameName)).getText().toString();

		if (gameName == null || gameName.isEmpty()) {
			Toast.makeText(this, "Type name of the game first!", Toast.LENGTH_SHORT).show();
			return;
		}

		String playerName = ((TextView) findViewById(R.id.playerName)).getText().toString();

		if (playerName == null || playerName.isEmpty()) {
			Toast.makeText(this, "Type your nick first!", Toast.LENGTH_SHORT).show();
			return;
		}

		String playerColor = dataProvider.getSelectedColor();

		if (playerColor == null || playerColor.isEmpty()) {
			Toast.makeText(this, "Choose your color first!", Toast.LENGTH_SHORT).show();
			return;
		}

		gameIntent.putExtra("game_name", gameName);
		gameIntent.putExtra("player_name", playerName);
		gameIntent.putExtra("player_color", playerColor);
		gameIntent.putExtra("isServer", 1);
		
		startActivity(gameIntent);
	}
}
